package com.multi.thread.chapter05.demo04;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Run1 {
	
	private static final SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static int runCount = 0;
	
	private static Timer timer = new Timer();
	
	static public class MyTask1 extends TimerTask {
		@Override
		public void run() {
			try {
				System.out.println("1 begin 运行了！时间为：" + ft.format(new Date()));
				Thread.sleep(5000);
				System.out.println("1   end 运行了！时间为：" + ft.format(new Date()));
				runCount++;
				if (runCount == 5)
					timer.cancel();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) throws ParseException {
		MyTask1 task = new MyTask1();
		String date = "2018-02-22 15:52:00";
		System.out.println("字符串1时间：" + date + ", 当前时间：" + ft.format(new Date()));
		timer.schedule(task, ft.parse(date), 3000);
	}

}
